package com.putong35.common.baseactfrag.fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView.LayoutManager;

import com.putong35.common.baseactfrag.CompState;
import com.putong35.common.baseactfrag.LoadState;
import com.putong35.common.baseactfrag.OnListDataUpdateListener;

import java.util.List;

public abstract class ListFragment extends RecyclerViewBaseFragment implements  OnListDataUpdateListener {

	private static final String TAG = ListFragment.class.getSimpleName();


	public ListFragment() {

	}


	@Override
	public LayoutManager createLayoutManager() {
		LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
		layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		return layoutManager;
	}



	@Override
	public void onListDataUpdateSuccess(List list) {
		if (!isDetached() && !isRemoving()) {
			if (list != null && list.size() > 0) {
				getAdapter().getData().addAll(list);
				getAdapter().notifyItemInserted(getAdapter().getData().size());
			}
			// state maybe refreshing, need to change state here
			if (getAdapter().getData().size() == 0) {
				setState(CompState.EMPTY);
			} else {
				setState(CompState.DATA);
			}
		}

		// TODO 状态要细化
		mCompHelper.setLoadState(LoadState.IDLE);
	}

	@Override
	public void onListDataUpdateFailed(int code, String msg) {
		setState(CompState.EMPTY_ERROR);
	}

}
